<?php
/**
 * Created by PhpStorm.
 * User: kobe.wang
 * Date: 2019/3/18
 * Time: 15:24
 */
namespace App\Common;

class TestSync extends \Thread
{
    private $file;

    public function __construct($file)
    {
        $this->file = $file;
    }

    public function run()
    {
        //发起一个上下文 获取同步锁
        $this->synchronized(function ($thread) {
            if (! $thread->done) {
                $thread->wait();//一个阻塞等待
            }
            //执行业务逻辑
            $c = file_get_contents($this->file);
            echo "before c = {$c}\n";
            $c = $c + 1;
            echo "after c = {$c}\n";
            file_put_contents($this->file, $c);
        }, $this);
    }
}